package softtech.apps.utility;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.telephony.SmsManager;
import android.util.Log;

public class ControlPhoneAction {

	/**
	 * Make a phone call
	 * @param context
	 * @param phoneNumber
	 */
	public static void makeCall(Context context , String phoneNumber ){
		Intent intent = new Intent(Intent.ACTION_CALL);

		intent.setData(Uri.parse("tel:" +  phoneNumber ));
		context.startActivity(intent);
	}
	
	/**
	 * Delete call log by number
	 * @param context
	 * @param number
	 */
	public static void deleteCallLogByNumber( Context context ,String number ) {   
	    String queryString="NUMBER="+number; 
	    context.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
	}
	
	/**
	 * send sms not save to inbox
	 * @param number
	 * @param message
	 */
	public static void sendSMS(String number, String message) {
		String phoneNumber = number;

		SmsManager smsManager = SmsManager.getDefault();

		if (message.length() < 150) {
			smsManager.sendTextMessage(phoneNumber, null, message, null, null);
		} else {
			String temp = message;
			while (temp.length() > 0) {
				smsManager.sendTextMessage(phoneNumber, null,
						temp.substring(0, 150), null, null);

				if (temp.length() > 150) {
					temp = temp.substring(0, 150);
				} else {
					temp = temp.substring(0);
				}

			}

		}
	}
	
	/**
	 * Delete sms by Phone Number
	 * @param context
	 * @param number
	 */
	public static void deleteSMS(Context context , String number) {
	    try {
	        Uri uriSms = Uri.parse("content://sms/inbox");
	        Cursor c = context.getContentResolver().query(uriSms,
	            new String[] { "_id", "thread_id", "address",
	                "person", "date", "body" }, null, null, null);

	        if (c != null && c.moveToFirst()) {
	            do {
	                long id = c.getLong(0);
	                //long threadId = c.getLong(1);
	                String address = c.getString(2);
	                //String body = c.getString(5);

	                if (address.equals(number)) {
	                    context.getContentResolver().delete(
	                        Uri.parse("content://sms/" + id), null, null);
	                }
	            } while (c.moveToNext());
	        }
	        Log.e("DELETE SMS","DELETE SMS "+number);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	/**
	 * Add a shortcut at laucher
	 * @param context
	 * @param newName
	 */
	public static void addShortcut(Context context  , String newName , int resId) {
	    // Intent to be send, when shortcut is pressed by user ("launched")
		Intent myLauncherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
		
		myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		// Create intent to fake 
		Intent intent = new Intent();
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, newName);
		intent.putExtra
		       (
		        Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
		        Intent.ShortcutIconResource.fromContext
		                                    (
		                                    		context.getApplicationContext(), 
		                                    		resId
		                                    )
		       );
		intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		
		context.getApplicationContext().sendBroadcast(intent);
	}
	
	/**
	 * delete shortcut at laucher
	 * @param context
	 * @param name
	 */
	public static void deleteShortCut(Context context , String name) {
		
		Intent myLauncherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
//		myLauncherIntent.setClassName(context.getPackageName(), ActivityLogin.class.getName());
		myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 
		Intent removeIntent = new Intent();
		removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
		removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		removeIntent.putExtra("duplicate", false);
		removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
		 
		context.sendBroadcast(removeIntent);
	}
	
}
